import request, { type AjaxResponse } from '../utils/request'
import type { AxiosResponse } from 'axios'
import type { LocationQueryValue } from 'vue-router'

/**
 * 登录
 * @param username 用户名
 * @param password 密码
 */
export const login = async function (
  username: string,
  password: string
): Promise<AjaxResponse<string>> {
  return request
    .post(
      '/passport/login',
      {
        username: username,
        password: password
      },
      {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }
    )
    .then((response: AxiosResponse<AjaxResponse<string>>) => {
      return response.data
    })
}

/**
 * 兑换票据
 * @param ticket
 */
export const ticket = async function (
  ticket: string | LocationQueryValue[]
): Promise<AjaxResponse<string>> {
  return request
    .get(`/passport/login/ticket?ticket=${ticket}`)
    .then((response: AxiosResponse<AjaxResponse<string>>) => {
      return response.data
    })
}
